/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */

package com.environment_pro.monitor.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.environment_pro.monitor.mapper.TbSystemDictMapper;
import com.environment_pro.monitor.model.TbSystemDict;
import com.environment_pro.monitor.service.TbSystemDictService;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageInfo;
import com.environment_pro.monitor.util.PageHelper.Page;
import com.environment_pro.monitor.util.StaticUtil;

/**
 * @author yangfeng 
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional  //此处不再进行创建SqlSession和提交事务，都已交由spring去管理了。
public class TbSystemDictServiceImpl implements TbSystemDictService {
	
	@Resource
	private TbSystemDictMapper mapper;

	//移除缓存
	@CacheEvict(value = "tbSystemDictCache",key="'id'+#id")
	public boolean delete(int id) {
		
		return mapper.delete(id);
	}

	@SuppressWarnings("unchecked")
	public Page<TbSystemDict> findAll(PageInfo pageInfo, TbSystemDict tbSystemDict) {
		PageHelper.startPage(pageInfo);  
		mapper.findAll(tbSystemDict);
		return PageHelper.endPage();
	}

	//使用缓存
	@Cacheable(value = "tbSystemDictCache",key="'id'+#id")
	public TbSystemDict findById(int id) {
		TbSystemDict tbSystemDict = mapper.findById(id);
		return tbSystemDict;
	}
	
	//使用缓存
	@Cacheable(value = "tbSystemDictCache",key="'code'+#dictCode")
	public TbSystemDict findByDictCode(String dictCode) {
		TbSystemDict tbSystemDict = mapper.findByDictCode(dictCode);
		return tbSystemDict;
	}
	
	//缓存数据
	//@CachePut(value = "tbSystemDictCache",key="'id'+#tbSystemDict.id")
	public void save(TbSystemDict tbSystemDict) {
		tbSystemDict.setAddTime(new Date());
		mapper.save(tbSystemDict);
	}
	//更新缓存
	@CacheEvict(value = "tbSystemDictCache",key="'id'+#tbSystemDict.id")
	public boolean update(TbSystemDict tbSystemDict) {

		return mapper.update(tbSystemDict);
	}
	
	/**
	 * 根据父节点查询子节点
	 */
	public List<TbSystemDict> findByPreDictCode(String preDictCode) {
		List<TbSystemDict> finList = mapper.findByPreDictCode(preDictCode);
		return finList;
	}

	//编码转化为名称
	public String code2name(String code) {
		TbSystemDict tbSystemDict = (TbSystemDict)StaticUtil.getCacheObject("tbSystemDictCache", String.valueOf("code"+code));
		if (tbSystemDict==null) {
			tbSystemDict = this.findByDictCode(code);
			StaticUtil.putCacheObject("tbSystemDictCache", String.valueOf("code"+code), tbSystemDict);
		}
		return tbSystemDict.getDictName();
	}

	//ID转化为名称
	public String id2name(int id) {
		TbSystemDict tbSystemDict = (TbSystemDict)StaticUtil.getCacheObject("tbSystemDictCache", String.valueOf("id"+id));
		if (tbSystemDict==null) {
			tbSystemDict = this.findById(id);
			StaticUtil.putCacheObject("tbSystemDictCache", String.valueOf("id"+id), tbSystemDict);
		}
		return tbSystemDict.getDictName();
	}

}